package main

import (
	"fmt"
	"runtime"
	"time"
)

func main() {
	done := make(chan struct{})
	go func() {
		for {
			_ = make([]byte, 1024*100) // 100KB 分配
			time.Sleep(10 * time.Millisecond)
		}
	}()

	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()

	for i := 0; i < 30; i++ {
		var m runtime.MemStats
		runtime.ReadMemStats(&m)
		fmt.Printf("GC次数: %d, 堆分配: %d MB, 暂停平均: %.2f µs\n",
			m.NumGC, m.HeapAlloc/1024/1024,
			float64(m.PauseTotalNs)/float64(m.NumGC+1)/1000)
		time.Sleep(time.Second)
	}
	close(done)
	fmt.Println("测试结束")
}
